Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ui-core]: add onSuccess and onError callback for useLoadData hook #3849

Merged
merged 4 commits into from
Sep 17, 2024

Conversation

ramprasadagarwal
Copy link
Collaborator

What changes were proposed in this pull request?

  • Add the onSuccess and onError callback for useLoadData hook. The callback can be used to trigger any dependant action including sequential API call.

How was this patch tested?

  • Added the unit test to test the calling of the respective callback.

Please review Hue Contributing Guide before opening a pull request.

Copy link
Collaborator

@bjornalm bjornalm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I reiterated my point on the "I" convention, please have a look. We can discuss if you want.

desktop/core/src/desktop/js/utils/hooks/useLoadData.ts Outdated Show resolved Hide resolved
@ramprasadagarwal ramprasadagarwal enabled auto-merge (squash) September 17, 2024 10:24
@@ -61,8 +66,16 @@ const useLoadData = <T, U = unknown>(url?: string, options?: IOptions<T, U>): IU
const fetchUrl = localOptions?.urlPrefix ? `${localOptions.urlPrefix}${url}` : url;
const response = await get<T, U>(fetchUrl, localOptions?.params, fetchOptions);
setData(response);
setError(undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setError not required as it is set above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is required in the edge case.

  1. First call made which resulted in error. Hence error is set to non-null and data is unchanged.
  2. Second call is made either by triggering refetch function or by changing and of the options. When its a success, the old error state is still set to non-null and data after second call is also set to non-null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I over-sighted the setError at line 63.
Thanks for flagging. I have removed it

} catch (error) {
setError(error instanceof Error ? error : new Error(error));
setData(undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old data will be removed do we want that?
lets say there is table already loaded and we are calling useLoadData hook in a interval. in case of api fails, we can throw error but should we remove the tables data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this makes sense.

@ramprasadagarwal ramprasadagarwal merged commit 9f0280e into master Sep 17, 2024
5 of 6 checks passed
@ramprasadagarwal ramprasadagarwal deleted the improv/use-load-data-hook branch September 17, 2024 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants